<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm"  ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标签字段" prop="tagField">
      <el-input v-model="dataForm.tagField" placeholder="标签字段"></el-input>
    </el-form-item>
    <el-form-item label="标签名称" prop="tagName">
      <el-input v-model="dataForm.tagName" placeholder="标签名称"></el-input>
    </el-form-item>
    <el-form-item label="标签描述" prop="tagDesc">
      <el-input v-model="dataForm.tagDesc" placeholder="标签描述"></el-input>
    </el-form-item>
    <el-form-item label="标签分组id" prop="tagGroupId">
      <el-input v-model="dataForm.tagGroupId" placeholder="标签分组id"></el-input>
    </el-form-item>
    <el-form-item label="更新方式，manual-手动，cron-定时" prop="updateMode">
      <el-input v-model="dataForm.updateMode" placeholder="更新方式，manual-手动，cron-定时"></el-input>
    </el-form-item>
    <el-form-item label="更新时间，如果选择定时更新则不能为空，实时即定时每分钟，通过解析cron得来" prop="updateCron">
      <el-input v-model="dataForm.updateCron" placeholder="更新时间，如果选择定时更新则不能为空，实时即定时每分钟，通过解析cron得来"></el-input>
    </el-form-item>
    <el-form-item label="标签类型 layer-自定义分层标签，preference-偏好标签，statistic-统计标签，distribution-行为分布标签，characteristic-首次末次特征标签" prop="tagType">
      <el-input v-model="dataForm.tagType" placeholder="标签类型 layer-自定义分层标签，preference-偏好标签，statistic-统计标签，distribution-行为分布标签，characteristic-首次末次特征标签"></el-input>
    </el-form-item>
    <el-form-item label="标签规则，json对象储存" prop="tagRule">
      <el-input v-model="dataForm.tagRule" placeholder="标签规则，json对象储存"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="删除标志" prop="deleteFlag">
      <el-input v-model="dataForm.deleteFlag" placeholder="删除标志"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tagId: 0,
          tagField: '',
          tagName: '',
          tagDesc: '',
          tagGroupId: '',
          updateMode: '',
          updateCron: '',
          tagType: '',
          tagRule: '',
          createTime: '',
          creator: '',
          deleteFlag: '',
          updateTime: ''
        },
        dataRule: {
          tagField: [
            { required: true, message: '标签字段不能为空', trigger: 'blur' }
          ],
          tagName: [
            { required: true, message: '标签名称不能为空', trigger: 'blur' }
          ],
          tagDesc: [
            { required: true, message: '标签描述不能为空', trigger: 'blur' }
          ],
          tagGroupId: [
            { required: true, message: '标签分组id不能为空', trigger: 'blur' }
          ],
          updateMode: [
            { required: true, message: '更新方式，manual-手动，cron-定时不能为空', trigger: 'blur' }
          ],
          updateCron: [
            { required: true, message: '更新时间，如果选择定时更新则不能为空，实时即定时每分钟，通过解析cron得来不能为空', trigger: 'blur' }
          ],
          tagType: [
            { required: true, message: '标签类型 layer-自定义分层标签，preference-偏好标签，statistic-统计标签，distribution-行为分布标签，characteristic-首次末次特征标签不能为空', trigger: 'blur' }
          ],
          tagRule: [
            { required: true, message: '标签规则，json对象储存不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          deleteFlag: [
            { required: true, message: '删除标志不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tagId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tagId) {
            this.$http({
              url: this.$http.adornUrl(`/tag/usertag/info/${this.dataForm.tagId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tagField = data.userTag.tagField
                this.dataForm.tagName = data.userTag.tagName
                this.dataForm.tagDesc = data.userTag.tagDesc
                this.dataForm.tagGroupId = data.userTag.tagGroupId
                this.dataForm.updateMode = data.userTag.updateMode
                this.dataForm.updateCron = data.userTag.updateCron
                this.dataForm.tagType = data.userTag.tagType
                this.dataForm.tagRule = data.userTag.tagRule
                this.dataForm.createTime = data.userTag.createTime
                this.dataForm.creator = data.userTag.creator
                this.dataForm.deleteFlag = data.userTag.deleteFlag
                this.dataForm.updateTime = data.userTag.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tag/usertag/${!this.dataForm.tagId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tagId': this.dataForm.tagId || undefined,
                'tagField': this.dataForm.tagField,
                'tagName': this.dataForm.tagName,
                'tagDesc': this.dataForm.tagDesc,
                'tagGroupId': this.dataForm.tagGroupId,
                'updateMode': this.dataForm.updateMode,
                'updateCron': this.dataForm.updateCron,
                'tagType': this.dataForm.tagType,
                'tagRule': this.dataForm.tagRule,
                'createTime': this.dataForm.createTime,
                'creator': this.dataForm.creator,
                'deleteFlag': this.dataForm.deleteFlag,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
